<div id="notebook" class="js-html">
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><a href="https://colab.research.google.com/github/Reagan1947/colba/blob/master/CreatingClass.ipynb" target="_parent"><img src="https://camo.githubusercontent.com/52feade06f2fecbf006889a904d221e6a730c194/68747470733a2f2f636f6c61622e72657365617263682e676f6f676c652e636f6d2f6173736574732f636f6c61622d62616467652e737667" alt="Open In Colab" data-canonical-src="https://colab.research.google.com/assets/colab-badge.svg"></a></p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[0]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># python3 version</span>
<span class="k">class</span> <span class="nc">SchoolStudent</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>  <span class="c1"># 是否需要继承object</span>
  <span class="k">pass</span>

<span class="c1"># python2 version</span>
<span class="k">class</span> <span class="nc">SchoolStudent</span><span class="p">(</span><span class="nb">object</span><span class="p">)</span>
  <span class="k">pass</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h1>如何创建一个类？<a class="anchor-link" href="#%E5%A6%82%E4%BD%95%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E7%B1%BB%EF%BC%9F">¶</a>
</h1>
<ol>
<li>
<code>class</code> 类标识符，表示创建的是一个类。</li>
<li>
<code>SchoolStudent</code> 类名称。
这里的类名称使用了驼峰命名法，为什么要使用驼峰命名法呢？
<br>这和后续创建实例有关
<br>当我们通过一个类创建一个新的对象的时候：
<br><code>school_student = SchoolStudent()</code>
<br>在python编码指南，<a href="https://https://www.python.org/dev/peps/pep-0008/">PEP8</a>中推荐使用下划线命名。
3.我们注意到在python2中类名后面是没有内容的。
<br>有人为python2写了一个倒计时页面：<a href="https://pythonclock.org/">https://pythonclock.org/</a>
4.回到python3的版本，后面的<code>(object)</code>表明了要继承的类。
<br>类的继承将在稍后讲到。但并不是每次都需要继承类，当没有类需要继承的时候
<br>另外，这里这里的<code>object</code> 继承有意义吗？
5.如何继承一个类？
<br>当然显而易见：<code>class SchoolStudent(student)</code>
<br>将类名放在括号内即可。</li>
</ol>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3>Python 为什么要继承 object 类？<a class="anchor-link" href="#Python-%E4%B8%BA%E4%BB%80%E4%B9%88%E8%A6%81%E7%BB%A7%E6%89%BF-object-%E7%B1%BB%EF%BC%9F">¶</a>
</h3>
<h4>首先我们引入两个概念：新式类和经典类<a class="anchor-link" href="#%E9%A6%96%E5%85%88%E6%88%91%E4%BB%AC%E5%BC%95%E5%85%A5%E4%B8%A4%E4%B8%AA%E6%A6%82%E5%BF%B5%EF%BC%9A%E6%96%B0%E5%BC%8F%E7%B1%BB%E5%92%8C%E7%BB%8F%E5%85%B8%E7%B1%BB">¶</a>
</h4>
<blockquote>
<p>Python 2.x中默认都是经典类，只有显式继承了object才是新式类
<br>Python 3.x中默认都是新式类，不必显式的继承object</p>
<h4>- 什么是新式类？<a class="anchor-link" href="#--%E4%BB%80%E4%B9%88%E6%98%AF%E6%96%B0%E5%BC%8F%E7%B1%BB%EF%BC%9F">¶</a>
</h4>
<p>在python2.x中给出如下定义：</p>
</blockquote>

<pre><code>class A(object):  # A是新式类，因为显式继承object
    pass

class B(A):  # B是新式类，因为B的基类A是新式类
    pass</code></pre>
<h4>- 什么是经典类(旧式类)?<a class="anchor-link" href="#--%E4%BB%80%E4%B9%88%E6%98%AF%E7%BB%8F%E5%85%B8%E7%B1%BB(%E6%97%A7%E5%BC%8F%E7%B1%BB)?">¶</a>
</h4>
<pre><code>class A:  # A是旧式类，因为没有显示继承object
    pass

class B(A):  # B是旧式类，因为B的基类A是旧式类
    pass</code></pre>
<p>可以明显的看出新式类继承<code>object</code>这个类。
它们有以下两点不同：</p>
<ol>
<li>保持class与type的统一对新式类的实例执行
<br>a.<strong>class</strong>与type(a)的结果是一致的，对于旧式类来说就不一样了。    </li>
<li>对于多重继承的属性搜索顺序不一样新式类是采用广度
<br>优先搜索，旧式类采用深度优先搜索。</li>
</ol>
<h4>1.首先我们来看第一点class与type的统一<a class="anchor-link" href="#1.%E9%A6%96%E5%85%88%E6%88%91%E4%BB%AC%E6%9D%A5%E7%9C%8B%E7%AC%AC%E4%B8%80%E7%82%B9class%E4%B8%8Etype%E7%9A%84%E7%BB%9F%E4%B8%80">¶</a>
</h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[0]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># 在python2.x(2.7)的环境下运行</span>
<span class="k">class</span> <span class="nc">A</span><span class="p">():</span><span class="k">pass</span>
<span class="k">class</span> <span class="nc">B</span><span class="p">():</span><span class="k">pass</span>

<span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">B</span><span class="p">()</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
  <span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>
  <span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">b</span><span class="p">))</span>
  <span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="n">b</span><span class="p">))</span>
  <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">__class__</span><span class="p">)</span>
  <span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">__class__</span><span class="p">)</span>
  <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">__class__</span> <span class="o">==</span> <span class="n">b</span><span class="o">.</span><span class="n">__class__</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[0]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># 在python2.x(2.7)的环境下运行</span>
<span class="c1"># 实例调用__class__属性时会指向该实例对应的类</span>
<span class="k">class</span> <span class="nc">A</span><span class="p">():</span><span class="k">pass</span>
<span class="k">class</span> <span class="nc">B</span><span class="p">():</span><span class="k">pass</span>

<span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">B</span><span class="p">()</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
  <span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>
  <span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">b</span><span class="p">))</span>
  <span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="n">b</span><span class="p">))</span>
  <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">__class__</span><span class="p">)</span>
  <span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">__class__</span><span class="p">)</span>
  <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">__class__</span> <span class="o">==</span> <span class="n">b</span><span class="o">.</span><span class="n">__class__</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>&lt;type 'instance'&gt;
&lt;type 'instance'&gt;
True
__main__.A
__main__.B
False
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[0]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># 在python2.x(2.7)的环境下运行 使用新式类</span>
<span class="k">class</span> <span class="nc">A</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span><span class="k">pass</span>
<span class="k">class</span> <span class="nc">B</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span><span class="k">pass</span>

<span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">B</span><span class="p">()</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
  <span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>
  <span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">b</span><span class="p">))</span>
  <span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="n">b</span><span class="p">))</span>
  <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">__class__</span><span class="p">)</span>
  <span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">__class__</span><span class="p">)</span>
  <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">__class__</span> <span class="o">==</span> <span class="n">b</span><span class="o">.</span><span class="n">__class__</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>&lt;class '__main__.A'&gt;
&lt;class '__main__.B'&gt;
False
&lt;class '__main__.A'&gt;
&lt;class '__main__.B'&gt;
False
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>观察到运行结果的区别</p>

</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[0]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># 使用python3.x环境运行</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># 在python2.x(2.7)的环境下运行</span>
<span class="k">class</span> <span class="nc">A</span><span class="p">():</span><span class="k">pass</span>
<span class="k">class</span> <span class="nc">B</span><span class="p">():</span><span class="k">pass</span>

<span class="n">a</span> <span class="o">=</span> <span class="n">A</span><span class="p">()</span>
<span class="n">b</span> <span class="o">=</span> <span class="n">B</span><span class="p">()</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</span><span class="p">:</span>
  <span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">a</span><span class="p">))</span>
  <span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">b</span><span class="p">))</span>
  <span class="nb">print</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="o">==</span> <span class="nb">type</span><span class="p">(</span><span class="n">b</span><span class="p">))</span>
  <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">__class__</span><span class="p">)</span>
  <span class="nb">print</span><span class="p">(</span><span class="n">b</span><span class="o">.</span><span class="n">__class__</span><span class="p">)</span>
  <span class="nb">print</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">__class__</span> <span class="o">==</span> <span class="n">b</span><span class="o">.</span><span class="n">__class__</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>&lt;class '__main__.A'&gt;
&lt;class '__main__.B'&gt;
False
&lt;class '__main__.A'&gt;
&lt;class '__main__.B'&gt;
False
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>观察到，继承<code>object</code>的新式类与python3的结果相同</p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h4>2.关于广度优先与深度优先搜索<a class="anchor-link" href="#2.%E5%85%B3%E4%BA%8E%E5%B9%BF%E5%BA%A6%E4%BC%98%E5%85%88%E4%B8%8E%E6%B7%B1%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7%B4%A2">¶</a>
</h4>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[0]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># 在python2.x(2.7)的环境下运行 新式类 广度优先</span>
<span class="k">class</span> <span class="nc">A</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>
    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">print</span> <span class="s2">"This is from A"</span>
<span class="k">class</span> <span class="nc">B</span><span class="p">(</span><span class="n">A</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>
<span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="n">A</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>
    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">print</span>  <span class="s2">"This is from C"</span>
<span class="k">class</span> <span class="nc">D</span><span class="p">(</span><span class="n">B</span><span class="p">,</span><span class="n">C</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>
<span class="n">fun</span> <span class="o">=</span> <span class="n">D</span><span class="p">()</span>
<span class="n">fun</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
<span class="c1"># B -&gt; A -&gt; C</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>This is from C
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[0]:</div>
<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="c1"># 在python2.x(2.7)的环境下运行 经典类 深度优先</span>
<span class="k">class</span> <span class="nc">A</span><span class="p">():</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>
    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">print</span> <span class="s2">"This is from A"</span>
<span class="k">class</span> <span class="nc">B</span><span class="p">(</span><span class="n">A</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>
<span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="n">A</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>
    <span class="k">def</span> <span class="nf">save</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="nb">print</span>  <span class="s2">"This is from C"</span>
<span class="k">class</span> <span class="nc">D</span><span class="p">(</span><span class="n">B</span><span class="p">,</span><span class="n">C</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>
<span class="n">fun</span> <span class="o">=</span> <span class="n">D</span><span class="p">()</span>
<span class="n">fun</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
<span class="c1"># B -&gt; C -&gt; A</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>This is from A
</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><img src="" alt="image.png"></p>

</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p><em>这里的 F为 D</em></p>

</div>
</div>
</div>
 

</div>